
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
import javax.imageio.*;


public class ImageManager
{
	// keys on the full path and filename of the image, data is a BufferedImage of the image
	private HashMap<String, BufferedImage> imagesMap; 

	private GraphicsConfiguration graphicConf;

	public ImageManager()
	{  
		imagesMap = new HashMap<String, BufferedImage>();
	
		// Acquiring the current Graphics Device and Graphics Configuration
		GraphicsEnvironment graphEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice graphDevice = graphEnv.getDefaultScreenDevice();
		graphicConf = graphDevice.getDefaultConfiguration();
	} 

	public BufferedImage loadImage(String imageName) {
		try {
			BufferedImage im =  ImageIO.read( new File (imageName) );
			int transparency = im.getColorModel().getTransparency();
			BufferedImage imageData = graphicConf.createCompatibleImage(im.getWidth(), im.getHeight(), transparency );

			// create a graphics context
			Graphics2D g2d = imageData.createGraphics();

			// copy image
			g2d.drawImage(im,0,0,null);
			g2d.dispose();
			
			imagesMap.put(imageName, imageData );
			System.out.println("Stored Image " + imageName + " in image storage.");
			
			return imageData;
		} 
		catch(IOException e) {
			System.out.println("Load Image error for " + imageName + "."); 
			return null;
		}
	}

	
	public BufferedImage getImage(String name)
	{
		BufferedImage imageData = (BufferedImage) imagesMap.get(name);
		if (imageData == null) {
			System.out.println("No image(s) stored under " + name);  
			return null;
		}

		return imageData;
	}
}
